package com.controller;


import com.domain.Article;
import com.domain.ArticleType;
import com.github.pagehelper.PageInfo;
import com.service.ArticleService;
import com.service.ArticleTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;
    @Autowired
    private ArticleTypeService articleTypeService;

    @RequestMapping("/list/{pageNumber}")
    public String selectAllPage(@PathVariable(value = "pageNumber") Integer pageNumber,
                                @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize, ModelMap map,
                                @RequestParam(value = "valueId", required = false) Integer valueId) {
        PageInfo<Article> pageInfo = articleService.selectAll(pageNumber, pageSize);
        map.put("pageInfo", pageInfo);
        return "list";
    }

    @RequestMapping("/queryListOne/{pageNumber}")
    public String queryListOne(@PathVariable(value = "pageNumber") Integer pageNumber,
                               @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize, ModelMap map,
                               @RequestParam(value = "valueId", required = false) Integer valueId) {

        PageInfo<Article> pageInfo = articleService.queryByOne(pageNumber, pageSize);
        map.put("pageInfo", pageInfo);
        return "list";
    }

    @RequestMapping("/queryListZero/{pageNumber}")
    public String queryListZero(@PathVariable(value = "pageNumber") Integer pageNumber,
                                @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize, ModelMap map,
                                @RequestParam(value = "valueId", required = false) Integer valueId) {


        PageInfo<Article> pageInfo = articleService.queryByZero(pageNumber, pageSize);
        map.put("pageInfo", pageInfo);
        return "list";
    }

    @GetMapping("/delete")
    public String delete(int id) {
        articleService.delete(id);
        return "redirect:/article/list/1";
    }

    @RequestMapping("/addUI")
    public ModelAndView addUI(ModelAndView modelAndView) {
        List<ArticleType> typeList = articleTypeService.selectAll();
        modelAndView.addObject("typeList", typeList);
        modelAndView.setViewName("add");
        return modelAndView;
    }

    @PostMapping(value = "add")
    public String add(Article article) {
        articleService.insert(article);
        return "redirect:/article/list/1";
    }

    @RequestMapping("/updateUI")
    public ModelAndView updateUI(ModelAndView modelAndView, int id) {

        Article article = articleService.selectById(id);
        modelAndView.addObject("articleById", article);

        List<ArticleType> typeList = articleTypeService.selectAll();
        modelAndView.addObject("typeList", typeList);
        modelAndView.setViewName("article_update");
        return modelAndView;
    }

    @RequestMapping("/detail")
    public ModelAndView detail(ModelAndView modelAndView, int id) {

        Article article = articleService.selectById(id);
        modelAndView.addObject("articleById", article);

        List<ArticleType> typeList = articleTypeService.selectAll();
        modelAndView.addObject("typeList", typeList);
        modelAndView.setViewName("detail");
        return modelAndView;
    }

    @PostMapping("/update")
    public String update(Article article) {
        articleService.update(article);
        return "redirect:/article/list/1";
    }

}
